package com.kaer.plugin.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

/**
 * Created by jxz on 2016/11/8.
 */

public class FileHelper {

    private static String TAG = "FileHelper";
    public static String noticeFileName = "notice.txt";
    private static File localNotice = new File(SdCardUtils.getLocalNoticePath());
    private static File terminalNotice = new File(SdCardUtils.getTerminalNoticePath());

    public static boolean isUserNoticeExists(){
        if(!localNotice.exists()){
            localNotice.mkdirs();
        }
        File[] files = new File(localNotice.getPath()).listFiles();
        if(files.length == 0){
            return false;
        }else{
            return true;
        }
    }

    public static String readUserNotice(){
        File noticeFile = null;
        if(!localNotice.exists()){
            localNotice.mkdirs();
        }
        File[] files = new File(localNotice.getPath()).listFiles();
        for(int i = 0; i < files.length; i++){
            String name = files[i].getName();
            if(name.endsWith(".txt")){
                //txtArray.add(files[i]);
                noticeFile = files[i];
            }
        }
        if(noticeFile != null && noticeFile.exists()) {
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(noticeFile), "GBK"/*"UTF-8"*/);//20161027_jinxiuzhen modify for Chinese.
                BufferedReader reader = new BufferedReader(isr);
                String readLine = "";
                StringBuffer buffer = new StringBuffer();
                while ((readLine = reader.readLine()) != null) {
                    buffer.append(readLine);
                }
                Log.i(TAG,"[jinxiuzhen]UserNotice is "+buffer);
                reader.close();
                return buffer.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static boolean isTerminalNoticeExists(){
        if(!terminalNotice.exists()){
            terminalNotice.mkdirs();
        }
        File[] files = new File(terminalNotice.getPath()).listFiles();
        if(files.length == 0){
            return false;
        }else{
            return true;
        }
    }

    public static String readTerminalNotice(){
        File noticeFile = null;
        if(!terminalNotice.exists()){
            terminalNotice.mkdirs();
        }
        File[] files = new File(terminalNotice.getPath()).listFiles();
        for(int i = 0; i < files.length; i++){
            String name = files[i].getName();
            if(name.endsWith(".txt")){
                //txtArray.add(files[i]);
                noticeFile = files[i];
            }
        }
        if(noticeFile != null && noticeFile.exists()) {
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(noticeFile), /*"GBK"*/"UTF-8");//20161027_jinxiuzhen modify for Chinese.
                BufferedReader reader = new BufferedReader(isr);
                String readLine = "";
                StringBuffer buffer = new StringBuffer();
                while ((readLine = reader.readLine()) != null) {
                    buffer.append(readLine);
                }
                Log.i(TAG,"[jinxiuzhen]TerminalNotice is "+buffer);
                reader.close();
                return buffer.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void writeTerminalNoticeToFile(String strcontent, String fileName) {
        //Log.d(TAG, "[jinxiuzhen]writeTxtToFile();");
        if (!terminalNotice.exists()) {
            terminalNotice.mkdir();
        }

        String strContent = strcontent + "\r\n";
        try {
            File file = new File(terminalNotice, fileName);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            } else {
                file.delete();
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(strContent.getBytes());
            raf.close();
        } catch (Exception e) {
            Log.e(TAG, "Error on write File:" + e);
        }
    }

    public static boolean deleteTerminalNoticeFile(){
        if(terminalNotice.exists()){
            File[] files = terminalNotice.listFiles();
            for(int i = 0; i < files.length; i++){
                File noticeFile = files[i];
                if(noticeFile.getName().equals(noticeFileName)){
                    noticeFile.delete();
                    return true;
                }
            }
        }
        return false;
    }
}
